package com.xsd.rabbitmq.topic;

import com.rabbitmq.client.*;
import com.xsd.rabbitmq.util.ConnectionUtil;

import java.io.IOException;

/**
 * @Description: 消费者
 * @Author: zhoufuzong
 * @Created: 2021-03-14 14:47:49
 * @Version: 1.1.0
 */

public class Consumer2 {
    public static void main(String[] args) throws Exception {
        Connection connection = ConnectionUtil.getConnection();
        Channel channel = connection.createChannel();
//        // 声明交换机
//        channel.exchangeDeclare(Producer.TOPIC_EXCHANGE, BuiltinExchangeType.TOPIC);
//        // 声明队列
//        channel.queueDeclare(Producer.TOPIC_QUEUE1,true,false,false,null);
//        // 队列绑定交换机
//        channel.queueBind(Producer.TOPIC_QUEUE2, Producer.TOPIC_EXCHANGE,"item.*");

        DefaultConsumer consumer = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                //路由key
                System.out.println("路由key为：" + envelope.getRoutingKey());
                //交换机
                System.out.println("交换机为：" + envelope.getExchange());
                //消息id
                System.out.println("消息id为：" + envelope.getDeliveryTag());
                //收到的消息
                System.out.println("消费者1-接收到的消息为：" + new String(body, "utf-8"));
            }
        };
        channel.basicConsume(Producer.TOPIC_QUEUE2,true,consumer);
    }

}
